import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { loginApi, registerApi, loginWithCodeApi } from '@/api/auth'

export const useUserStore = defineStore(
  'userStore',
  () => {
    const count = ref(0)
    const doubleCount = computed(() => count.value * 2)
    function increment() {
      count.value++
    }
    const name = ref('Card Store信息')
    const token = ref('')
    const userInfo = ref<any>(null)
    
    const setToken = (value: string) => {
      token.value = value
      // 同步到localStorage
      if (value) {
        localStorage.setItem('token', value)
      } else {
        localStorage.removeItem('token')
      }
    }
    
    const getToken = () => {
      return token.value || localStorage.getItem('token') || ''
    }
    
    // 检查是否已登录
    const isLoggedIn = computed(() => {
      return !!getToken()
    })
    
    // 登录方法
    const login = async (credentials: { email: string; password: string }) => {
      try {
        const res: any = await loginApi(credentials)
        if (res.code === 0 && res.data.token) {
          setToken(res.data.token)
          userInfo.value = {
            uid: res.data.uid,
            email: credentials.email
          }
          return { success: true, data: res.data }
        }
        return { success: false, message: res.msg || '登录失败' }
      } catch (error: any) {
        return { success: false, message: error.response?.data?.msg || '登录请求失败' }
      }
    }
    
    // 验证码登录方法
    const loginWithCode = async (credentials: { email: string; code: string }) => {
      try {
        const res: any = await loginWithCodeApi(credentials)
        if (res.code === 0 && res.data.token) {
          setToken(res.data.token)
          userInfo.value = {
            uid: res.data.uid,
            email: credentials.email
          }
          return { success: true, data: res.data }
        }
        return { success: false, message: res.msg || '登录失败' }
      } catch (error: any) {
        return { success: false, message: error.response?.data?.msg || '登录请求失败' }
      }
    }
    
    // 注册方法
    const register = async (registerData: {
      email: string;
      password: string;
      confirmPassword: string;
      veryCode: string;
    }) => {
      try {
        const res = await registerApi(registerData)
        if (res.code === 0) {
          return { success: true, data: res.data }
        }
        return { success: false, message: res.msg || '注册失败' }
      } catch (error: any) {
        return { success: false, message: error.response?.data?.msg || '注册请求失败' }
      }
    }
    
    // 登出方法
    const logout = () => {
      setToken('')
      userInfo.value = null
    }
    
    return { 
      count, 
      doubleCount, 
      increment, 
      name, 
      token, 
      userInfo,
      setToken, 
      getToken, 
      isLoggedIn,
      login,
      loginWithCode,
      register,
      logout
    }
  },
  //持久化配置
  {
    persist: {
      key: 'userStore',
      storage: localStorage,
      paths: ['token', 'userInfo']
    },
  }
)
